# -*- coding: utf-8 -*-
"""
longitude, latitude -> path, row of WRS
WRS = World Reference System

Day time - Descending order of row.
Night time - Ascending order of row.

Created on Wed Jun 10 13:53:40 2020

@author: Farman
"""

import numpy
import joblib


def c(a, b):
    p = a[0] * b[1] - a[1] * b[0]
    p = p < 0            
    return p


class WRS:
    '''
    longitude, latitude -> path, row in WRS
    
    WRS = World Reference System.
    '''
    def __init__(self, dump_file):
        try:
            (self.path_row, 
             self.control_lon_lat, 
             self.up_left_lon_lat, 
             self.up_right_lon_lat, 
             self.bottom_left_lon_lat, 
             self.bottom_right_lon_lat) = joblib.load(dump_file)
            
            self.vector0 = self.up_right_lon_lat - self.up_left_lon_lat
            self.vector1 = self.bottom_right_lon_lat - self.up_right_lon_lat
            self.vector2 = self.bottom_left_lon_lat - self.bottom_right_lon_lat
            self.vector3 = self.up_left_lon_lat - self.bottom_left_lon_lat
            
            
            def round_deg(a):
                while a > 180:
                    a -= 360
                
                while a < -180:
                    a += 360
                
                return a
            
            
            self.vector0 = [[round_deg(lon), round_deg(lat)]
                            for [lon, lat] in self.vector0]
            
            self.vector1 = [[round_deg(lon), round_deg(lat)]
                            for [lon, lat] in self.vector1]
            
            self.vector2 = [[round_deg(lon), round_deg(lat)]
                            for [lon, lat] in self.vector2]
            
            self.vector3 = [[round_deg(lon), round_deg(lat)]
                            for [lon, lat] in self.vector3]
            
            self.vector0 = numpy.array(self.vector0)
            self.vector1 = numpy.array(self.vector1)
            self.vector2 = numpy.array(self.vector2)
            self.vector3 = numpy.array(self.vector3)
            
        except:
            print('')
            print('Fatal Error:')
            print('----------------------------------------------------------')
            print('    Fail to load file "%s" while initialize WRS.'%dump_file)
            print("")
            
            (self.path_row, 
             self.control_lon_lat, 
             self.up_left_lon_lat, 
             self.up_right_lon_lat, 
             self.bottom_left_lon_lat, 
             self.bottom_right_lon_lat) = (numpy.array([]) for n in range(6))
        
        return
    
    
    def get_path_row(self, lon_deg, lat_deg):
        test_pos = numpy.array([lon_deg, lat_deg])
        
        self.vec0 = test_pos - self.up_left_lon_lat
        self.vec1 = test_pos - self.up_right_lon_lat
        self.vec2 = test_pos - self.bottom_right_lon_lat
        self.vec3 = test_pos - self.bottom_left_lon_lat
        
        def cross_prod(a, b):
            p = a[:,0] * b[:,1] - a[:,1] * b[:,0]
            p = p < 0            
            return p
        
        cp0 = cross_prod(self.vector0, self.vec0)
        cp1 = cross_prod(self.vector1, self.vec1)
        cp2 = cross_prod(self.vector2, self.vec2)
        cp3 = cross_prod(self.vector3, self.vec3)
        
        cp = cp0 * cp1 * cp2 * cp3
        
        path_row_match = []
        
        for n in range(len(cp)):
            if cp[n]:
                path_row_match.append(list(self.path_row[n]))
        return path_row_match


#=============================================================================

        
if __name__ == '__main__':
    lon, lat = 101, 10
    wrs = WRS('wrs_control_points.joblib')
    cp = wrs.get_path_row(lon, lat)
    print(lon, lat, '->', cp)
    
    for n in range(len(wrs.path_row)):
        v = wrs.vector1[n][0]
        
        if abs(v) < -180:
            print(n, v)
    
    